from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Enum, Boolean
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from enum import Enum as PyEnum
from ..core.database import Base

class PermissionType(PyEnum):
    READ = "read"
    WRITE = "write"
    DELETE = "delete"
    SHARE = "share"

class ShareLevel(PyEnum):
    PRIVATE = "private"        # 私有，仅自己可见
    DEPARTMENT = "department"  # 部门内可见
    PUBLIC = "public"         # 公开，所有人可见
    CUSTOM = "custom"         # 自定义权限

class FilePermission(Base):
    __tablename__ = "file_permissions"
    
    id = Column(Integer, primary_key=True, index=True)
    file_id = Column(Integer, ForeignKey("files.id"), nullable=False)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    permission = Column(Enum(PermissionType), nullable=False)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    
    # 关系
    file = relationship("File", back_populates="permissions")
    user = relationship("User", back_populates="file_permissions")

# 文件夹部门权限
class FolderDepartmentPermission(Base):
    __tablename__ = "folder_department_permissions"
    
    id = Column(Integer, primary_key=True, index=True)
    folder_id = Column(Integer, ForeignKey("files.id"), nullable=False)
    department_id = Column(Integer, ForeignKey("departments.id"), nullable=False)
    can_read = Column(Boolean, default=True)
    can_write = Column(Boolean, default=False)
    can_delete = Column(Boolean, default=False)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    
    # 关系
    folder = relationship("File", foreign_keys=[folder_id])
    department = relationship("Department")

# 文件夹用户权限
class FolderUserPermission(Base):
    __tablename__ = "folder_user_permissions"
    
    id = Column(Integer, primary_key=True, index=True)
    folder_id = Column(Integer, ForeignKey("files.id"), nullable=False)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    can_read = Column(Boolean, default=True)
    can_write = Column(Boolean, default=False)
    can_delete = Column(Boolean, default=False)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    
    # 关系
    folder = relationship("File", foreign_keys=[folder_id])
    user = relationship("User")

class FileShare(Base):
    __tablename__ = "file_shares"
    
    id = Column(Integer, primary_key=True, index=True)
    file_id = Column(Integer, ForeignKey("files.id"), nullable=False)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    share_token = Column(String(255), unique=True, nullable=False)
    share_level = Column(Enum(ShareLevel), default=ShareLevel.PRIVATE)
    expires_at = Column(DateTime(timezone=True), nullable=True)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    
    # 关系
    file = relationship("File", back_populates="shares")
    user = relationship("User", back_populates="shared_files")